package panda.query.tree;

import panda.metainfo.StaticInfo;
import panda.query.scan.Scanner;
import panda.query.scan.TableScanner;
import panda.query.struct.Attribute;
import panda.record.Schema;
import panda.record.TableToken;
import panda.server.Panda;
import panda.transaction.Transaction;

public class TableNode implements QueryTreeNode {
	
	private Transaction tx;
	private TableToken tt;
	private StaticInfo si = null;
//	private Schema sch;
	
	
	public TableNode(TableToken tt, Transaction tx) {
		this.tx = tx;
		this.tt = tt;
//		sch = tt.getSchema();
	}
	
	public Scanner open() {
		return new TableScanner(tt, tx);
	}
	 
	
//	public static long time; 
	
	public TableNode(String tblname, Transaction tx) {
		this.tx = tx;
		tt = Panda.getMetaInfoManager().getTableToken(tblname, tx);
		
//		time -= System.currentTimeMillis();
//		si = Panda.getMetaInfoManager().getStaticInfo(tt.getTableName(), tt, tx);
//		time += System.currentTimeMillis();
//		sch = tt.getSchema();
	}

	public int blockAccessed() {
		if (si == null) si = Panda.getMetaInfoManager().getStaticInfo(tt.getTableName(), tt, tx);
		return si.getBlockAccessed();
	}

	public int distinctSize(Attribute attr) {
		if (si == null) si = Panda.getMetaInfoManager().getStaticInfo(tt.getTableName(), tt, tx);
		return si.getDistinctSize(attr.getAttributeName());
	}

	public int outputSize() {
		if (si == null) si = Panda.getMetaInfoManager().getStaticInfo(tt.getTableName(), tt, tx);
		return si.getOutputSize();
	}

	public Schema getSchema() {
		return tt.getSchema();
	}
	
	public TableToken getTableToken() {
		return tt;
	}

}
